package com.zy.mydemo.api.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zy.mydemo.api.service.UserService;
import com.zy.mydemo.common.entity.UserEntity;
import com.zy.mydemo.common.entity.UserParamEntity;
import com.zy.mydemo.common.mapper.UserMapper;
import com.zy.mydemo.common.utils.PasswordUtils;
import com.zy.mydemo.common.utils.ResultUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @Author:zhongyong
 * @Description：
 * @Date：Create in 2020/04/29
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public UserEntity findUser(UserEntity user) {
        if (user == null) {
            return null;
        }
        if (user.getId() == 0 && StringUtils.isEmpty(user.getLoginId())) {
            return null;
        }
        return userMapper.findUser(user);
    }

    @Override
    public ResultUtils getList(UserParamEntity param, int pageIndex, int pageSize) {
        pageIndex = pageIndex <= 0 ? 1 : pageIndex;
        pageSize = pageSize <= 0 ? 10 : pageSize;

        PageHelper.startPage(pageIndex, pageSize);

        List<UserEntity> list = userMapper.getList(param);

        PageInfo<UserEntity> pageInfo = new PageInfo<>(list);

        return ResultUtils.success("成功", pageInfo);
    }

    @Override
    public ResultUtils saveUser(UserEntity user) {
        if (user == null) {
            return ResultUtils.error("参数异常");
        }

        if (user.getId() == 0) {
            return ResultUtils.error("userId不能为空");
        }

        //判断用户是否存在
        if (userMapper.findUser(new UserEntity() {{
            setId(user.getId());
        }}) == null) {
            return ResultUtils.error("用户不存在");
        }

        if (!StringUtils.isEmpty(user.getLoginId())) {
            //更改loginId
            if (userMapper.findUser(new UserEntity() {{
                setLoginId(user.getLoginId());
            }}) != null) {
                return ResultUtils.error("loginId已存在");
            }
        }

        if (StringUtils.isEmpty(user.getPassword())) {
            user.setSalt(null);
        } else {
            if (StringUtils.isEmpty(user.getSalt())) {
                user.setSalt(PasswordUtils.getSalt(20));
            }

            //给密码加密
            user.setPassword(PasswordUtils.encrypt(user.getPassword(), user.getSalt()));
        }

        if (userMapper.updateUser(user) <= 0) {
            return ResultUtils.error("数据库异常,保存失败");
        }

        return ResultUtils.success("成功");
    }


    @Override
    public ResultUtils addUser(UserEntity user) {
        if (user == null) {
            return ResultUtils.error("参数异常");
        }

        user.setId(0);

        if (StringUtils.isEmpty(user.getLoginId())) {
            return ResultUtils.error("登录ID不能为空");
        }
        if (StringUtils.isEmpty(user.getPassword())) {
            return ResultUtils.error("登录密码不能为空");
        }

        if (StringUtils.isEmpty(user.getUserName())) {
            user.setUserName(user.getLoginId());
        }

        if (StringUtils.isEmpty(user.getSalt())) {
            user.setSalt(PasswordUtils.getSalt(20));
        }

        //判断这个loginId是否已经存在
        if (userMapper.findUser(user) != null) {
            return ResultUtils.error("用户已存在");
        }

        //为密码加密
        user.setPassword(PasswordUtils.encrypt(user.getPassword(), user.getSalt()));

        int id = userMapper.insertUser(user);
        if (id <= 0) {
            return ResultUtils.error("数据库异常,添加失败");
        }

        return ResultUtils.success("成功", id);
    }

//    @Override
//    @Transactional
//    public boolean testTransactional() {
//        userMapper.insertUser(new UserEntity("大爷1", "123"));
//        userMapper.insertUser(new UserEntity("大爷2", "123"));
//        userMapper.insertUser(new UserEntity("大爷3", "123"));
//        userMapper.insertUser(new UserEntity("大爷4", "123"));
//        userMapper.insertUser(new UserEntity("大爷5", "123"));
//        userMapper.insertUser(new UserEntity("大爷aaaaaaaaaaaaaa", "123"));
//        userMapper.insertUser(new UserEntity("大爷6", "123"));
//        userMapper.insertUser(new UserEntity("大爷7", "123"));
//        return true;
//    }
}
